package com.src.iseclive.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.src.iseclive.integration.lifecycle.ActivityLifecycleable;
import com.src.iseclive.mvp.presenter.IPresenter;
import com.src.iseclive.mvp.view.IView;
import com.trello.rxlifecycle2.android.ActivityEvent;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;

/**
 * @author liujiancheng
 * @name IsecLive
 * @class name：com.src.isec.base
 * @class Activity基类
 * 完成presenter的注入、butterknife的绑定以及RxLifecycle的生命周期绑定
 * @time 2018/3/16 17:59
 * @change
 * @chang time
 * @class describe
 */

public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements
        ActivityLifecycleable {
    protected final String TAG = this.getClass().getSimpleName();
    private final BehaviorSubject<ActivityEvent> mLifecycleSubject = BehaviorSubject.create();
    private Unbinder mUnbinder;

    protected Toolbar mToolbar;

    @Inject
    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null

    /**
     * @author liujiancheng
     * @time 2018/3/19  16:03
     * @describe 用于Rxjava绑定生命周期
     */
    @NonNull
    @Override
    public Subject<ActivityEvent> provideLifecycleSubject() {
        return mLifecycleSubject;
    }


    /**
     * @author liujiancheng
     * @time 2018/3/19  16:03
     * @describe 初始化 View
     */
    protected abstract int initView(@Nullable Bundle savedInstanceState);


    /**
     * @author liujiancheng
     * @time 2018/3/19  16:02
     * @describe 初始化数据
     * 等同onCreate()
     */
    protected abstract void initialize(@Nullable Bundle savedInstanceState);


    /**
     * @return
     * @author liujiancheng
     * @time 2018/3/19  16:00
     * @describe 这个 Activity 是否会使用 Fragment,框架会根据这个属性判断是否注册
     * {@link FragmentManager.FragmentLifecycleCallbacks}
     * 如果返回{@code false},那意味着这个 Activity 不需要绑定 Fragment,那你再在这个 Activity 中绑定继承于
     * {@link BaseFragment} 的 Fragment 将不起任何作用
     * @see com.src.iseclive.integration.ActivityLifecycle Fragment生命周期的注册过程
     */
    public boolean useFragment() {

        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            //将Presenter关联View
            if (mPresenter != null && this instanceof IView)
                mPresenter.attachView((IView) this);
            int layoutResID = initView(savedInstanceState);
            //如果initView返回0,框架则不会调用setContentView(),当然也不会 Bind ButterKnife
            if (layoutResID != 0) {
                setContentView(layoutResID);
                //绑定到butterknife
                mUnbinder = ButterKnife.bind(this);
            }
            initialize(savedInstanceState);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
        this.mUnbinder = null;
        if (mPresenter != null)
            mPresenter.onDestroy();//释放资源
        this.mPresenter = null;
    }


    /**
     * @author liujiancheng
     * @time 2018/3/20  10:46
     * @describe 当前Activity是否需要自定义Toolbart
     * 默认为flase
     */
    public boolean isCustomToolbar() {


        return false;
    }

    /**
     * @author liujiancheng
     * @time 2018/3/20  10:50
     * @describe 设置当前Toolbar
     * 设置Toolbar是在onstart()生命周期中调用
     * 请勿在oncreate()中使用Toolbar
     */
    public void setToolbar(Toolbar toolbar) {

        mToolbar = toolbar;
    }

}
